package com.easychat.controller;

import com.easychat.annotation.GlobalInterceptor;
import com.easychat.common.PageResult;
import com.easychat.common.Result;
import com.easychat.entity.AppUpdate;
import com.easychat.entity.query.AppUpdateQuery;
import com.easychat.service.AppUpdateService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import java.io.IOException;

/**
 * (AppUpdate)表控制层
 *
 * @author makejava
 * @since 2024-10-14 16:39:29
 */
@RestController("adminAppUpdateController")
@RequestMapping("admin")
public class AdminAppUpdateController {
    /**
     * 服务对象
     */
    @Resource
    private AppUpdateService appUpdateService;

    /**
     * 分页查询版本信息
     * @param query
     * @return
     */
    @GetMapping("/loadUpdateList")
    @GlobalInterceptor(checkAdmin = true)
    public Result<PageResult> loadUpdateList(AppUpdateQuery query){
        query.setOrderBy("id desc");
        PageResult pageResult = appUpdateService.queryListByPage(query);
        return Result.ok(pageResult);
    }

    /**
     * 添加修改版本信息
     * @param id
     * @param version
     * @param updateDesc
     * @param fileType
     * @param outerLink
     * @param file
     * @return
     * @throws IOException
     */
    @GetMapping("/saveUpdate")
    @GlobalInterceptor(checkAdmin = true)
    public Result saveUpdate(Integer id,
                             @NotEmpty String version,
                             @NotEmpty String updateDesc,
                             @NotEmpty Integer fileType,
                             String outerLink,
                             MultipartFile file) throws IOException {
        AppUpdate appUpdate = new AppUpdate();
        appUpdate.setId(id);
        appUpdate.setVersion(version);
        appUpdate.setUpdateDesc(updateDesc);
        appUpdate.setFileType(fileType);
        appUpdate.setOuterLink(outerLink);
        appUpdateService.saveUpdate(appUpdate,file);
        return Result.ok();
    }

    /**
     * 删除版本信息
     * @param id
     * @return
     */
    @PostMapping("/delUpdate")
    @GlobalInterceptor(checkAdmin = true)
    public Result delUpdate(@NotEmpty Integer id){
        appUpdateService.deleteById(id);
        return Result.ok();
    }

    /**
     * 发布版本
     * @param id
     * @return
     */
    @PostMapping("/postUpdate")
    @GlobalInterceptor(checkAdmin = true)
    public Result postUpdate(@NotEmpty Integer id,@NotEmpty Integer status,String grayscaleUid){
        appUpdateService.postUpdate(id,status,grayscaleUid);
        return Result.ok();
    }




}

